﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PingPong
{
    public class Ball
    {
        //Current position of the ball
        public int[] BallPosition { get; set; }

        //Next position of the ball -> where do we move. Calculated by Collision(directionX, directionY, currentPosition)
        public char BallChar { get; set; }
        //True if the ball is moving to the left; If we have reached the limits of the field, we make the var= false and start moving the ball to the left
        public bool GoingLeft { get; set; }
        //True if the ball is moving up; If we have reached the limits of the field, we make the var= false and start moving the ball downwards
        public bool GoingUp { get; set; }

        public PlayGround Playground { get; set; }

        public Ball(char ballCharValue, PlayGround playground)
        {
            BallChar = ballCharValue;
            Playground = playground;
            BallPosition = new int[2];
        }
        public void MoveBall(Player player1, Player aiPlayer, List<Obstacle> obstacleList)
        //int firstPlayerResult, int secondPlayerResult, int firstPlayerPosition, int secondPlayerPosition, int firstPlayerpadSize, int secondPlayerpadSize
        {
            //Moving UpDown
            if (GoingUp)
            {
                BallPosition[1]--;
            }
            else
            {
                BallPosition[1]++;
            }
            //Moving LeftRight
            if (GoingLeft)
            {
                BallPosition[0]--;
            }
            else
            {
                BallPosition[0]++;
            }

            //Obstacle collision by Rado
            if (obstacleList.Any(obst => ((obst.PositionX == BallPosition[0] + 1) && (obst.PositionY == BallPosition[1])) || (obst.PositionX == BallPosition[0] - 1) && (obst.PositionY == BallPosition[1])))
            {
                PingPong.SoundsOnBounces();
                if (GoingLeft)
                {
                    GoingLeft = false;
                }
                else
                {
                    GoingLeft = true;
                }
                if (GoingUp)
                {
                    GoingUp = false;
                }
                else
                {
                    GoingUp = true;
                }
            }
            //COLISON
            else if (BallPosition[1] == 1)//top side collision
            {
                GoingUp = false;
                PingPong.SoundsOnBounces();
            }
            else if (BallPosition[1] == Playground.Height - 3)//bottom side collision
            {
                GoingUp = true;
                PingPong.SoundsOnBounces();
            }
            else if (BallPosition[0] == Playground.Width - 1)//right side collision
            {
                if (GoingUp == true)
                {
                    ResetBall(); //Edited by Yordan
                    obstacleList.Clear(); // edit by Rado
                    player1.Result++; //Edited by Yordan
                    GoingUp = true; // edit by Svetlin
                    GoingLeft = true;
                    PingPong.SoundsOnWin();
                }
                else
                {
                    ResetBall(); //Edited by Yordan
                    obstacleList.Clear(); // edit by Rado
                    player1.Result++; //Edited by Yordan
                    GoingUp = false; // edit by Svetlin
                    GoingLeft = true;
                    PingPong.SoundsOnWin();
                }
            }
            else if (BallPosition[0] == 0)//left side collision
            {
                ResetBall();
                obstacleList.Clear(); // edit by Rado
                aiPlayer.Result++;
                GoingUp = true;
                GoingLeft = true;
                PingPong.SoundsOnLoss();
            }
            else if (BallPosition[0] < 2)
            {
                if (BallPosition[1] >= player1.PlayerPad.Position[1]
                    && BallPosition[1] < player1.PlayerPad.Position[1] + player1.PlayerPad.Size)
                {
                    GoingLeft = false;
                    PingPong.SoundsOnHits();
                    if (GoingUp == true) // edit Svetlin
                    {
                        GoingUp = true;
                    }
                    else
                    {
                        GoingUp = false;
                    }
                }
                // edit by Svetlin
                //PingPong.SoundsOnHits();
            }
            else if (BallPosition[0] > Console.WindowWidth - 3) // edit by Yordan
            {
                if (BallPosition[1] >= aiPlayer.PlayerPad.Position[1]
                    && BallPosition[1] < aiPlayer.PlayerPad.Position[1] + aiPlayer.PlayerPad.Size)
                {
                    GoingLeft = true;
                    PingPong.SoundsOnHits();
                    if (GoingUp == true) // edit Svetlin
                    {
                        GoingUp = true;
                    }
                    else
                    {
                        GoingUp = false;
                    }
                }
            }
        }

        public void ResetBall()
        {
            BallPosition[0] = Console.WindowWidth / 2;
            BallPosition[1] = Console.WindowHeight / 2;
        }
    }
}


//if (ballPosition[0] >= Console.WindowWidth - 4)
//{
//    if (ballPosition[1] >= secondPlayerPosition
//        && ballPosition[1] < secondPlayerPosition + secondPlayerpadSize)
//    {
//        goingLeft = true;
//    }
//}